﻿using System;
using System.Windows;
using System.Windows.Controls;
using TheOliver.DigitalSignage.Client.Interfaces;
using System.Windows.Threading;

namespace CeBIT2010
{
    public partial class CebitSlidePlaceholder : UserControl, ISlidePlaceholder
    {
        public CebitSlidePlaceholder()
        {
            InitializeComponent();

            this.Loaded += (s1, e1) =>
                {
                    // Date Label Timer
                    DispatcherTimer dayTimer = new DispatcherTimer();
                    dayTimer.Tick += (s, e) =>
                    {
                        UpdateDateLabel();
                    };
                    dayTimer.Interval = new TimeSpan(0, 1, 0);
                    dayTimer.Start();

                    // Current and Next Session Timer
                    DispatcherTimer sessionTimer = new DispatcherTimer();
                    sessionTimer.Tick += (s, e) =>
                    {
                        UpdatePreview();
                    };
                    sessionTimer.Interval = new TimeSpan(0, 0, 5);
                    sessionTimer.Start();

                    UpdateDateLabel();
                    UpdatePreview();
                };
        }

        private void UpdatePreview()
        {
            AdminApp.Web.AllSlots nextSession = Configuration.NextSessionOfToday;
            AdminApp.Web.AllSlots currentSession = Configuration.CurrentSession;

            if (nextSession != null)
            {
                _nextSession.Visibility = System.Windows.Visibility.Visible;
                _nextSession.DataContext = nextSession;
                _nextSession.DisplayType = DisplayType.Next;
            }
            else
            {
                _nextSession.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (currentSession != null)
            {
                _currentSession.Visibility = System.Windows.Visibility.Visible;
                _currentSession.DataContext = currentSession;
                _currentSession.DisplayType = DisplayType.Current;
            }
            else
            {
                _currentSession.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void UpdateDateLabel()
        {
            _dateLabel.Text = Configuration.CurrentDateTime.Date.ToShortDateString();
        }

        #region ISlidePlaceholder Members

        public FrameworkElement Slide
        {
            get
            {
                return _content.Content as FrameworkElement;
            }
            set
            {
                _content.Content = value;
            }
        }

        public event EventHandler HideSlideCompleted;
        public event EventHandler ShowSlideCompleted;

        private void OnHideSlideCompleted()
        {
            if (HideSlideCompleted != null)
            {
                HideSlideCompleted(this, EventArgs.Empty);
            }
        }

        private void OnShowSlideCompleted()
        {
            if (ShowSlideCompleted != null)
            {
                ShowSlideCompleted(this, EventArgs.Empty);
            }
        }

        public void ShowSlide()
        {
            _showSlideStoryboard.Completed += _showSlideStoryboard_Completed;
            _showSlideStoryboard.Begin();
        }

        void _showSlideStoryboard_Completed(object sender, EventArgs e)
        {
            _showSlideStoryboard.Completed -= _showSlideStoryboard_Completed;
            OnShowSlideCompleted();
        }

        public void HideSlide()
        {
            _hideSlideStoryboard.Completed += _hideSlideStoryboard_Completed;
            _hideSlideStoryboard.Begin();
        }

        void _hideSlideStoryboard_Completed(object sender, EventArgs e)
        {
            _hideSlideStoryboard.Completed -= _hideSlideStoryboard_Completed;
            OnHideSlideCompleted();
        }

        #endregion
    }
}
